import os
from langchain_community.llms.tongyi import Tongyi
from langchain_community.agent_toolkits.load_tools import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory


os.environ["SERPAPI_API_KEY"] = 'db166b810c6b85674b6ceab3bd4e10d5048e1ba837db1c0d962ad91b34558805'
llm = Tongyi()

# 记忆组件
memory = ConversationBufferMemory(
    memory_key="chat_history",
)
# 定义tool
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# 定义agent
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    memory=memory,  # 记忆组件
    verbose=True,
)

def aiDoctor(question):
    answer = agent.run(question)
    print(answer)
    return answer


if __name__ == '__main__':
    print(aiDoctor("宠物猫太瘦了怎么办"))